package com.yc.config;

import com.alibaba.csp.sentinel.datasource.WritableDataSource;

import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class SentinelWritableDataSourceConfig {

    private final String serverAddr = "127.0.0.1:8848";
    private final String username = "nacos";
    private final String password = "nacos";
    private final String namespace = "test147";
    private final String groupId = "DEFAULT_GROUP";

    @PostConstruct
    public void init() throws Exception {
        // 流控规则
        WritableDataSource<List<FlowRule>> flowDs = new NacosWritableDataSourceJackson<>(
                serverAddr, username, password, namespace,
                "sentinelFlowRule", groupId);
        WritableDataSourceRegistry.registerFlowDataSource(flowDs);

        // 熔断规则
        WritableDataSource<List<DegradeRule>> degradeDs = new NacosWritableDataSourceJackson<>(
                serverAddr, username, password, namespace,
                "sentinelDegradeRule", groupId);
        WritableDataSourceRegistry.registerDegradeDataSource(degradeDs);

        // 系统规则
        WritableDataSource<List<SystemRule>> systemDs = new NacosWritableDataSourceJackson<>(
                serverAddr, username, password, namespace,
                "sentinelSystemRule", groupId);
        WritableDataSourceRegistry.registerSystemDataSource(systemDs);

        // 授权规则
        WritableDataSource<List<AuthorityRule>> authorityDs = new NacosWritableDataSourceJackson<>(
                serverAddr, username, password, namespace,
                "sentinelAuthorityRule", groupId);
        WritableDataSourceRegistry.registerAuthorityDataSource(authorityDs);
//
//        // 热点参数规则
//        WritableDataSource<List<ParamFlowRule>> paramFlowDs = new NacosWritableDataSourceJackson<>(
//                serverAddr, username, password, namespace,
//                "sentinelParamFlowRule", groupId);
//        WritableDataSourceRegistry.registerParamFlowDataSource(paramFlowDs);


    }
}
